package main

import (
	"chatproject/models"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {

	db, err := gorm.Open(mysql.Open("linan:Linan2002@@tcp(39.105.203.233:3306)/chatproject?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	// Migrate the schema
	err = db.AutoMigrate(&models.Message{})
	if err != nil {
		fmt.Println(err)
	}
	message := models.Message{}
	message.Content = "这是我的第一条消息"
	db.Create(&message)
	_ = db.AutoMigrate(&models.Contact{})
	_ = db.AutoMigrate(&models.GroupBasic{})

	// Create
	//user := &models.UserBasic{}
	//user.Name = "first_user"
	//db.Create(user)

	//Read
	//fmt.Println(db.First(user, 1)) // find product with integer primary key
	////db.First(user, "code = ?", "D42") // find product with code D42
	//
	//Update - update product's price to 200
	//db.Model(user).Update("Password", "linan2002@")
	// 更新 id=1 的用户密码
	// Update - update multiple fields
	//db.Model(user).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
	//db.Model(user).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	//Delete - delete product
	//db.Delete(user, 1)
	//db.Delete(user, 2)
}
